package com.example.spinning.service;

import com.example.spinning.bean.Organ;
import com.example.spinning.bean.Role;
import com.example.spinning.bean.User;
import com.example.spinning.mapper.RoleMapper;
import com.example.spinning.mapper.RolesMapper;
import com.example.spinning.mapper.UserMapper;
import com.example.spinning.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Random;

/**
 * 用户
 */
@Service
@Transactional
public class UsersService {
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    RoleMapper roleMapper;
    @Autowired
    PasswordEncoder passwordEncoder;

//    public List<User> getUserByNickname(String nickname) {
//        List<User> list = usersMapper.getUserByNickname(nickname);
//        return list;
//    }

    /**
     * @param user
     * @return 0表示成功
     * 1表示用户名重复
     * 2表示失败
     *
     */
//    用户注册
    public int reg(User user) {
//        查询是否有该user
        User loadUserByUsername = usersMapper.loadUserByUsername(user.getUsername());
        if (loadUserByUsername != null) {
            return 1;
        }
        User loadUserByNickname = usersMapper.loadUserByNickname(user.getNickname());

        if (loadUserByNickname != null) {
//产生0到99之间的随机数
            while(true){
                Random random = new Random();
                int num = random.nextInt(100);
                user.setNickname(user.getNickname()+""+num);
                User loadUserByNickname_ = usersMapper.loadUserByNickname(user.getNickname());
                if(loadUserByNickname_==null){
                    break;
                }
            }

        }

//        设置默认密码
        if(user.getPassword()==null){
            user.setPassword("123");
        }
        //插入用户,插入之前先对密码进行加密
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        long result = usersMapper.reg(user);
        //配置用户的角色，默认都是普通用户
//        String[] roles = new String[]{"2"};

        String role = user.getRole();
        Role role_=new Role();
        role_.setRole(role);
        int id=roleMapper.getRoleId(role_);

        String[] roles = new String[]{id+""};
        int i = roleMapper.addRoles(roles, user.getId());
        boolean b = i == roles.length && result == 1;
        if (b) {
            return 0;
        } else {
            return 2;
        }
    }

    //查询user
    public List<User> getSpecificUsers(int page,int count,User user){
        int start = (page-1)*count;
        return usersMapper.getSpecificUsers(start,count,user);
    }
//查询全部用户
    public List<User> getAllUsers() {
        return usersMapper.getAllUsers();
    }

    //删除用户信息
    public boolean deleteUsersByIds(String ids) {
        String[] split = ids.split(",");
        int result = usersMapper.deleteUsersByIds(split);
        if(result>0){
            int result2 = usersMapper.deleteUsersRolesByUid(split);
        }
        return result == split.length;
    }

//    修改user
    public int updateUserById(User user) {
//        如果包含角色，先更新用户的角色
        if(user.getRole()!=null){
            String role = user.getRole();
            Role role_=new Role();
            role_.setRole(role);
            int rid=roleMapper.getRoleId(role_);
            int uid =  Integer.parseInt(user.getId()+"");
//            int ur_id=roleMapper.getUserRoleId(rid,uid);
            roleMapper.updateUserRole(rid,uid);

        }

        return usersMapper.updateUserById(user);
    }

//    使用nickname查询用户与权限
    public List<User> getUserByNickname(String nickname) {
    return usersMapper.getUserByNickname(nickname);
}

    //   按条件查询用户数量
    public int getUserCountByCondition(User user) {
        return usersMapper.getUserCountByCondition(user);
    }





}
